package com.wuxinwudai.android.hardware;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import java.util.ArrayList;

/**
 * ShakeSensorMonitor 类为手机晃动感应监视器
 * @author 吾心无待 于2016年07月20日
 */
public class ShakeSensorMonitor implements SensorEventListener {
    private SensorManager mSensorManager;
    private ArrayList<OnShakeListener> mOnShakeListeners = new ArrayList<>(10);
    private float lowX, lowY, lowZ;
    /** 监听是否启动 */
    private boolean isDetector;

    /**
     * 构造函数，初始化 ShakeSensorMonitor 类的一个新实例
     * @param context 上下文对象
     */
    public ShakeSensorMonitor(Context context) {
        // 获取传感器管理服务
        mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float x = event.values[SensorManager.DATA_X];
            float y = event.values[SensorManager.DATA_Y];
            float z = event.values[SensorManager.DATA_Z];
            float FILTERING_VALUE = 0.1f;
            lowX = x * FILTERING_VALUE + lowX * (1.0f - FILTERING_VALUE);
            lowY = y * FILTERING_VALUE + lowY * (1.0f - FILTERING_VALUE);
            lowZ = z * FILTERING_VALUE + lowZ * (1.0f - FILTERING_VALUE);
            float highX = x - lowX;
            float highY = y - lowY;
            float highZ = z - lowZ;
            if (highX >= 10 || highY >= 10 || highZ >= 10) {
                for(OnShakeListener l:mOnShakeListeners){
                    l.onShake();
                }
                stop();
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    /**
     * 启动摇晃检测--注册监听器
     */
    public void start() {
        if(isDetector) {
            return;
        }
        if (mSensorManager == null) {
            throw new UnsupportedOperationException();
        }
        // 第一个参数是Listener，第二个参数是所得传感器类型，第三个参数值获取传感器信息的频率
        boolean success = mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        if (!success) {
            throw new UnsupportedOperationException("硬件不支持晃动功能，或者请检查是否注册权限！");
        }
        isDetector = true;
    }

    /**
     * 停止摇晃检测--取消监听器
     */
    public void stop() {
        if (mSensorManager != null) {
            mSensorManager.unregisterListener(this);
        }
        isDetector = false;
    }

    /**
     * 获取是否正在监听中
     * @return 是否正在监听中
     */
    public boolean isDetector() {
        return isDetector;
    }

    /**
     * 当摇晃事件发生时，接收通知
     */
    public interface OnShakeListener {
        /**
         * 当手机晃动时被调用
         */
        void onShake();
    }

    /**
     * 添加监听对象
     * @param listener 添加监听事件
     */
    public void addOnShakeListener(OnShakeListener listener) {
        this.mOnShakeListeners.add(listener);
    }

    /**
     * 移除监听对象
     * @param listener 监听对象
     */
    public void removeOnShakeListener(OnShakeListener listener){
        this.mOnShakeListeners.remove(listener);
    }
}
